import { defineStore } from "pinia";
import { computed, reactive, ref } from "vue";
import { pageParams, roleListInfo } from "@/api/permissions/roleApi/interface.ts";
import { fetchRoleListAPI } from "@/api/permissions/roleApi/role.ts";
import { role } from "@/api/permissions/userApi/interface.ts";

const useRoleStore = defineStore("roleStore", () => {
  /* 角色信息列表 */
  const roleListInfo = ref<roleListInfo>({});
  /* 角色列表 */
  const roleList = computed<role[]>(() => roleListInfo.value.records);
  /* 分页参数 */
  const pageParams = reactive<pageParams>({
    page: 1,
    limit: 10,
    roleName: undefined,
  });

  const total = computed<number>(() => roleListInfo.value.total);

  /* 发送请求获取角色信息列表的回调函数 */
  const fetchRoleList = async () => {
    const { data } = await fetchRoleListAPI(pageParams);
    roleListInfo.value = data;
  };
  return {
    roleListInfo,
    pageParams,
    roleList,
    total,
    fetchRoleList,
  };
});

export default useRoleStore;
